package com.googlesource.gerrit.plugins.examples.servlet;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.net.util.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class HttpClientUtil {
    private static final String prefer = "http://localhost:8081/";

    //用户密码方式
    private static String getBaseAuth(){
        Base64 base64 = new Base64();
        //"admin:admin"是jira账号拼密码
        return "Basic " + base64.encodeToString(("username:password").getBytes(StandardCharsets.UTF_8));
    }

    //cookie方式
    private static String getCookies(HttpServletRequest request){
        Cookie[] cookie = request.getCookies();
        StringBuilder cookies = new StringBuilder();
        for (int i=0;i<cookie.length;i++) {
            cookies.append(cookie[i].getName()).append("=").append(cookie[i].getValue());
            if(i+1 != cookie.length) {
                cookies.append("; ");
            }
        }
        return cookies.toString();
    }
    public static String sendGet(String url, HttpServletRequest request) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(prefer + url);
        httpGet.setHeader("Accept", "application/json");
        httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
        //httpGet.setHeader("Authorization", getBaseAuth());
        httpGet.setHeader("Cookie", getCookies(request));
        CloseableHttpResponse response = client.execute(httpGet);
        HttpEntity msg = response.getEntity();
        return EntityUtils.toString(msg);
    }
    public static void sendPut(String url, String jsonStr, HttpServletRequest request) throws IOException {
        //JSONObject jsonObject = new JSONObject();
        CloseableHttpClient build = HttpClientBuilder.create().build();
        HttpPut httpPut = new HttpPut(prefer + url);
        httpPut.setHeader(HTTP.CONTENT_TYPE, "application/json");
        //httpPut.setHeader("Authorization", getBaseAuth());
        httpPut.setHeader("Cookie", getCookies(request));
        StringEntity se = new StringEntity(jsonStr, "UTF-8");  // 中文乱码解决
        se.setContentType("application/json");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httpPut.setEntity(se);
        HttpResponse httpResponse = build.execute(httpPut);
        System.out.println(httpResponse.getStatusLine().getStatusCode());
    }
    public static void sendPost(String url, String jsonStr, HttpServletRequest request) throws IOException {
        CloseableHttpClient build = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(prefer + url);
        httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
        httpPost.addHeader("Cookie", getCookies(request));
        StringEntity se = new StringEntity(jsonStr, "UTF-8");  // 中文乱码解决
        se.setContentType("application/json");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httpPost.setEntity(se);
        HttpResponse httpResponse = build.execute(httpPost);
        System.out.println(httpResponse.getStatusLine().getStatusCode());
    }
    public static JSONObject doPost(String url, String jsonStr) throws IOException {
        JSONObject jsonObject = new JSONObject();
        CloseableHttpClient build = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
        StringEntity se = new StringEntity(jsonStr, "UTF-8");  // 中文乱码解决
        se.setContentType("application/json");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httpPost.setEntity(se);
        HttpResponse httpResponse = build.execute(httpPost);
        jsonObject.put("message", EntityUtils.toString(httpResponse.getEntity()));
        jsonObject.put("status", httpResponse.getStatusLine().getStatusCode());
        return jsonObject;
    }

}
